﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SuperCalc
{
    /// <summary>
    /// 计算器主窗体
    /// </summary>
    public partial class MainForm : Form
    {
        #region 属性
        /// <summary>
        /// 下方输入框的运算数最高长度
        /// 因为只能显示31位，而且要留下正负数的符号的位置
        /// </summary>
        private const int downTextMaxLong = 23;

        /// <summary>
        /// 结果显示控件的上层文本
        /// </summary>
        private string TopText
        {
            get
            {
                return ucResultsTheBox1.TopText;
            }
            set
            {
                ucResultsTheBox1.TopText = value;
            }
        }

        /// <summary>
        /// 结果显示控件的下层文本
        /// </summary>
        private string DownText
        {
            get
            {
                return ucResultsTheBox1.DownText;
            }
            set
            {
                ucResultsTheBox1.DownText = value;
            }
        }

        /// <summary>
        /// 用于标识控件ucResultsTheBox1的文本位置
        /// </summary>
        private enum TextLocation
        {
            /// <summary>
            /// 上层
            /// </summary>
            top,
            /// <summary>
            /// 下层
            /// </summary>
            down
        }

        /// <summary>
        /// 运算符
        /// </summary>
        private enum operatorEnum
        {
            /// <summary>
            /// 没有输入运算符
            /// </summary>
            none,
            /// <summary>
            /// 加法
            /// </summary>
            add,
            /// <summary>
            /// 减法
            /// </summary>
            subtraction,
            /// <summary>
            /// 乘法
            /// </summary>
            multiplication,
            /// <summary>
            /// 除法
            /// </summary>
            division,
            /// <summary>
            /// 开方
            /// </summary>
            ExtractionOfARoot,
            /// <summary>
            /// %
            /// </summary>
            Percent,
            /// <summary>
            /// 1/x
            /// </summary>
            oneDivideX
        }

        /// <summary>
        /// 当前记忆的数字
        /// </summary>
        private decimal _memoryNumber = 0;

        /// <summary>
        /// 当前记忆的数字
        /// </summary>
        public decimal MemoryNumber
        {
            get { return _memoryNumber; }
            set { _memoryNumber = value; }
        }

        /// <summary>
        /// 第一个操作数
        /// </summary>
        private decimal _firstOperand = 0;

        /// <summary>
        /// 第一个操作数
        /// </summary>
        public decimal FirstOperand
        {
            get { return _firstOperand; }
            set { _firstOperand = value; }
        }

        /// <summary>
        /// 第二个操作数
        /// </summary>
        private decimal _secondOperand = 0;
        /// <summary>
        /// 第二个操作数
        /// </summary>
        public decimal SecondOperand
        {
            get { return _secondOperand; }
            set { _secondOperand = value; }
        }

        /// <summary>
        /// 当前运算符
        /// </summary>
        private operatorEnum _currentOperator = operatorEnum.none;
        /// <summary>
        /// 当前运算符
        /// </summary>
        private operatorEnum CurrentOperator
        {
            get { return _currentOperator; }
            set { _currentOperator = value; }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }
        #endregion

        #region 事件
        /// <summary>
        /// 清除目前记忆的数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMC_Click(object sender, EventArgs e)
        {
            //todo:记忆数字的使用需要重新调整
            MemoryNumber = 0;
        }
        /// <summary>
        /// Memory Recall的意思，将当前计算出来的数字呈现出来。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMR_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 无视目前记忆多少数字，直接以当前数字取代记忆中的数字。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMS_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// M+：记忆当前数字，「加入」累加数字当中。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMAdd_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 记忆当前数字，以负数的形式「加入」累加数字当中。换句话说，就是把总数扣除当前数字的意思。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMMinus_Click(object sender, EventArgs e)
        {

        }

        private void btnBackspace_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 清除当前输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCE_Click(object sender, EventArgs e)
        {

        }

        private void btnC_Click(object sender, EventArgs e)
        {

        }

        private void btnPlusOrMinus_Click(object sender, EventArgs e)
        {

        }

        private void btnExtractionOfARoot_Click(object sender, EventArgs e)
        {

        }

        private void btnPercent_Click(object sender, EventArgs e)
        {

        }

        private void btn1DivideX_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 加号键点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddition_Click(object sender, EventArgs e)
        {
            CurrentOperator = operatorEnum.add;
        }

        /// <summary>
        /// 减法按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubtraction_Click(object sender, EventArgs e)
        {
            //更新当前的运算符
            CurrentOperator = operatorEnum.subtraction;
        }

        /// <summary>
        /// 乘法按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMultiplication_Click(object sender, EventArgs e)
        {
            //更新当前的运算符
            CurrentOperator = operatorEnum.multiplication;
        }

        /// <summary>
        /// 除法按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDivision_Click(object sender, EventArgs e)
        {
            //更新当前的运算符
            CurrentOperator = operatorEnum.division;
        }

        private void btnResult_Click(object sender, EventArgs e)
        {

        }

        private void btn0_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("0");
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("1");
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("2");
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("3");
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("4");
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("5");
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("6");
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("7");
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("8");
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            ConcatDownOperand("9");
        }

        private void btnDecimalPoint_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 方法
        /// <summary>
        /// 拼接字符串到计算器显示框
        /// </summary>
        /// <param name="textLocation">文本位于显示框的位置</param>
        /// <param name="concatString">需要拼接的字符串</param>
        private void ConcatString(TextLocation textLocation, string concatString)
        {
            //

            if (textLocation == TextLocation.top)
            {
                TopText = TopText + concatString;
            }
            else if (textLocation == TextLocation.down)
            {
                DownText = DownText + concatString;
            }
            else
            {

            }
        }

        /// <summary>
        /// 拼接操作数
        /// </summary>
        /// <param name="operand">运算数字</param>
        private void ConcatDownOperand(string operand)
        {
            //限制下层文本框的长度
            //因为下层文本框用于显示运算数字，并只能显示固定的长度
            if (ucResultsTheBox1.DownTextLong < downTextMaxLong)
            {
                ConcatString(TextLocation.down, operand);
            }
            else  //超过长度不做任何处理，不需要提示，因为界面上可以表现出来
            {

            }
        }
        #endregion
    }
}
